package com.dao.impl;

import com.dao.Admins_AccountsDao;
import com.dao.BaseDao;
import com.pojo.Admins_Accounts;
import com.service.Admins_AccountsService;
import com.service.impl.Admins_AccountsServiceImpl;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import static com.util.TryParseUtil.tryParseInt;
public class Admins_AccountsDaoImpl implements Admins_AccountsDao {
    public List<Admins_Accounts> getAllAdmins_Accounts(String query) {
        BaseDao baseDao = new BaseDao();
        List<Admins_Accounts> list=new ArrayList<>();
        try(Connection conn = baseDao.getConn();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from admins_accounts where STATUS=0")) {
            while (rs.next()) {
                Admins_Accounts admins_accounts = new Admins_Accounts(
                        rs.getInt("ADMIN_ID"),
                        rs.getString("USER_NAME"),
                        rs.getString("PASSWORD"),
                        rs.getString("PHONE"),
                        rs.getString("EMAIL"),
                        rs.getString("NAME"),
                        rs.getString("ROLE"),
                        rs.getInt("STATUS")
                );

                list.add(admins_accounts);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public int Add(Admins_Accounts admins_accounts){
        String sql="insert into admins_accounts(USER_NAME,PASSWORD,NAME,EMAIL,ROLE,PHONE) values(?,?,?,?,?,?)";
        return BaseDao.ExcuteUpdate(
                sql,
                admins_accounts.getUSER_NAME(),
                admins_accounts.getPASSWORD(),
                admins_accounts.getNAME(),
                admins_accounts.getEMAIL(),
                admins_accounts.getROLE(),
                admins_accounts.getPHONE()
        );
    }
    public int Delete(int id){
        String sql="update admins_accounts set STATUS=1 where ADMIN_ID=?";
        return BaseDao.ExcuteUpdate(sql,id);
    }


    public  int count(String query)  {
        BaseDao baseDao = new BaseDao();
        String sql = "select count(1) from admins_accounts";
        if(!query.equals("")){
            sql= sql = "select count(1) from admins_accounts where " +
                    "ADMIN_ID = ?  " +
                    "or USER_NAME like ? " +
                    "or NAME like ? " +
                    "or EMAIL like ? " +
                    "or ROLE like ?" +
                    "or PHONE like ?" ;
        }
        ResultSet rs = null;
        try(Connection conn = baseDao.getConn();
            PreparedStatement pstmt = conn.prepareStatement(sql)//查询全部数据
        ){
            if(!query.equals("")){
                if(tryParseInt(query)){
                    pstmt.setObject(1,query);
                }else {
                    pstmt.setObject(1,0);
                }//若是数字则执行id查询
                pstmt.setObject(2,"%"+query+"%");
                pstmt.setObject(3,"%"+query+"%");
                pstmt.setObject(4,"%"+query+"%");
                pstmt.setObject(5,"%"+query+"%");
                pstmt.setObject(6,"%"+query+"%");
            }//条件不为空则执行条件查询
            rs = pstmt.executeQuery();
            while (rs.next()){
                return rs.getInt(1);
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return 0;
    }
    public  List<Admins_Accounts> pages(int index,int size,String query){
        List<Admins_Accounts> list = new ArrayList<>();
        BaseDao baseDao = new BaseDao();
        String sql=null ;
        if(query.equals("")){
            sql = "select * from admins_accounts limit ?,?";
        }else {
            sql = "select * from admins_accounts where " +
                    "ADMIN_ID = ?  " +
                    "or USER_NAME like ? " +
                    "or NAME like ? " +
                    "or EMAIL like ?" +
                    "or ROLE like ?" +
                    "or PHONE like ?" +
                    " limit ?,?";
        }

        Connection conn = baseDao.getConn();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Admins_AccountsService gradeService = new Admins_AccountsServiceImpl();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setObject(1,(index-1)*size);
            pstmt.setObject(2,size);
            if(!query.equals("")){
                if(tryParseInt(query)){
                    pstmt.setObject(1,query);
                }else {
                    pstmt.setObject(1,0);
                }
                pstmt.setObject(2,"%"+query+"%");
                pstmt.setObject(3,"%"+query+"%");
                pstmt.setObject(4,"%"+query+"%");
                pstmt.setObject(5,"%"+query+"%");
                pstmt.setObject(6,"%"+query+"%");
                pstmt.setObject(7,(index-1)*size);
                pstmt.setObject(8,size);
            }
            rs = pstmt.executeQuery();
            while (rs.next()){
                Admins_Accounts admins = new Admins_Accounts(
                        rs.getInt("ADMIN_ID"),
                        rs.getString("USER_NAME"),
                        rs.getString("PASSWORD"),
                        rs.getString("PHONE"),
                        rs.getString("EMAIL"),
                        rs.getString("NAME"),
                        rs.getString("ROLE"),
                        rs.getInt("STATUS")
                );
                list.add(admins);
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            baseDao.closeAll(conn,pstmt,rs);
        }
        return list;
    }
    public Admins_Accounts GetById(int id){
        BaseDao baseDao = new BaseDao();
        String sql = "select * from admins_accounts where ADMIN_ID=?";
        ResultSet rs = null;
        try(Connection conn = baseDao.getConn();
            PreparedStatement pstmt = conn.prepareStatement(sql)//查询全部数据
        ){
            pstmt.setObject(1,id);
            rs = pstmt.executeQuery();
            while (rs.next()){
                Admins_Accounts admins = new Admins_Accounts(
                        rs.getInt("ADMIN_ID"),
                        rs.getString("USER_NAME"),
                        rs.getString("PASSWORD"),
                        rs.getString("PHONE"),
                        rs.getString("EMAIL"),
                        rs.getString("NAME"),
                        rs.getString("ROLE"),
                        rs.getInt("STATUS")
                );
                return admins;
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return null;
    }
    public int Update(Admins_Accounts admins_accounts){
        String sql="update admins_accounts set USER_NAME=?,PASSWORD=?,NAME=?,EMAIL=?,ROLE=?,PHONE=? where ADMIN_ID=?";
        return BaseDao.ExcuteUpdate
                (
                        sql,
                        admins_accounts.getUSER_NAME(),
                        admins_accounts.getPASSWORD(),
                        admins_accounts.getNAME(),
                        admins_accounts.getEMAIL(),
                        admins_accounts.getROLE(),
                        admins_accounts.getPHONE(),
                        admins_accounts.getADMIN_ID()
                );
    }

    public  boolean Login(String username,String password){
        BaseDao baseDao = new BaseDao();
        String sql = "select count(1) from admins_accounts where USER_NAME=? and PASSWORD=?";
        ResultSet rs = null;
        try(Connection conn = baseDao.getConn();
            PreparedStatement pstmt = conn.prepareStatement(sql)//查询全部数据
        ){
            pstmt.setObject(1,username);
            pstmt.setObject(2,password);
            rs = pstmt.executeQuery();
            rs.next();
            if(rs.getInt(1)>0){
                return true;
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return false;
    }

    public  int getUidByName(String Uname){
        try(Connection conn=BaseDao.getConn();
        Statement stmt=conn.createStatement();
        ResultSet rs=stmt.executeQuery("select ADMIN_ID from admins_accounts where USER_NAME='"+Uname+"'")){
            if(rs.next()){
                return rs.getInt("ADMIN_ID");
            }

        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return 0;
    }
    public  String getUnameByPhone(String phone){
        try(Connection conn=BaseDao.getConn();
            Statement stmt=conn.createStatement();
            ResultSet rs=stmt.executeQuery("select USER_NAME from admins_accounts where PHONE='"+phone+"'")){
            if(rs.next()){
                return rs.getString("USER_NAME");
            }

        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return null;
    }
    public String getRole( int uid){
        try(Connection conn=BaseDao.getConn();
            Statement stmt=conn.createStatement();
            ResultSet rs=stmt.executeQuery("select ROLE from admins_accounts where ADMIN_ID="+uid)){
            if(rs.next()){
                return rs.getString("ROLE");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    public boolean CheckUname(String Uname){
        try(Connection conn=BaseDao.getConn();
            Statement stmt=conn.createStatement();
            ResultSet rs=stmt.executeQuery("select count(1) from admins_accounts where USER_NAME='"+Uname+"'")){
            if(rs.next()){
                if(rs.getInt(1)>0){
                    return false;
                }
            }

        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return true;
    }
    public boolean CheckPhone(String phone){
        try(Connection conn=BaseDao.getConn();
            Statement stmt=conn.createStatement();
            ResultSet rs=stmt.executeQuery("select count(1) from admins_accounts where PHONE='"+phone+"'")){
            if(rs.next()){
                if(rs.getInt(1)>0){
                    return false;
                }
            }

        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return true;
    }
}
